﻿'========================================================================
'功能:IP對應位置資料匯入維護作業 
'作者:shihyu
'日期: 2011/06/10
'========================================================================
Imports System.Data
Partial Class WebForm_OtherData_IPLoactionMaintain
    Inherits BasePage
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hiUID.Value = ActualUID
            hiDBName.Value = dbName

            If Not ChkProgramRight(1) Then '查詢
                Me.btnQuery.Visible = False
            End If

            If Not ChkProgramRight(2) Then '新增
                Me.btnInsert.Visible = False
            End If

            If Not ChkProgramRight(3) Then '刪除
                Me.gvIPLocData.Columns(0).Visible = False
            End If
            If Not ChkProgramRight(4) Then '匯入
                btnImport.Visible = False
                lblImport.Visible = False
                fuImport.Visible = False
                hlImport.Visible = False

            End If
        End If
        ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(Me.btnImport)
    End Sub
    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.gvIPLocData.DataBind()
    End Sub
    Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        Me.txtpopIP.Text = "..."
        Me.txtpopEndIP.Text = "..."
        Me.txtpopLocation.Text = ""

    End Sub
    ''' <summary>
    ''' 檢查ip資料是否存在系統中
    ''' </summary>
    ''' <param name="IP_Beg">開始IP</param>
    ''' <param name="IP_End">結束IP</param>
    ''' <param name="Location">位置</param>
    ''' <param name="Site">廠區</param>
    ''' <returns>false代表存在系統,true代表未存在系統</returns>
    ''' <remarks></remarks>
    Protected Function IPCheck(ByVal IP_Beg As String, ByVal IP_End As String, ByVal Location As String, ByVal Site As String) As Boolean
        Dim bbl As AssetManage.BBL.IPLocationDataBBL = New AssetManage.BBL.IPLocationDataBBL()
        If bbl.IPExist(IP_Beg, IP_End, Location, Site, dbName) Then
            Return False
        End If
        Return True
    End Function
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        ddlFactory.SelectedValue = ddlpopFactory.SelectedValue
        If Me.IPCheck(Me.txtpopIP.Text.Trim(), Me.txtpopEndIP.Text.Trim(), Me.txtpopLocation.Text.Trim(), Me.ddlpopFactory.SelectedValue) = False Then

            ShowMessageForAjax(btnSave, GetLocalResourceObject("IPExist"))
            mpIns.Show()
            Return
        End If
        '
        Me.dbIPLocData.Insert()


        Me.txtIP.Text = "..."

        Me.txtLocation.Text = ""
        Me.gvIPLocData.DataBind()
    End Sub
    Protected Sub btnImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImport.Click
        If Not fuImport.HasFile Then
            ShowMessage("請選擇上傳的檔案")
        ElseIf IO.Path.GetExtension(fuImport.PostedFile.FileName).ToUpper() <> ".XLS" AndAlso IO.Path.GetExtension(fuImport.PostedFile.FileName).ToUpper() <> ".XLSX" Then
            ShowMessage("匯入失敗!!請選擇副檔名(xls、xlsx)檔案!!")
        End If
        '上傳暫存檔
        Dim strGuid As Guid = Guid.NewGuid()
        Dim newName As String = Server.MapPath("~/") & "Imports\" & strGuid.ToString() & IO.Path.GetExtension(fuImport.PostedFile.FileName).ToUpper()
        fuImport.PostedFile.SaveAs(newName)

        Dim ExcelImportCheck As New ExcelImportCheck()
        Dim dt As New DataTable

        '檢查工作表名稱
        If ExcelImportCheck.LoadData(dt, newName, "Sheet1", True) = False Then
            Return
        End If

        For i As Integer = 0 To dt.Columns.Count - 1
            Select Case i
                Case 0
                    dt.Columns(0).ColumnName = "Site"
                Case 1
                    dt.Columns(1).ColumnName = "Location"
                Case 2
                    dt.Columns(2).ColumnName = "IPBeg"
                Case 3
                    dt.Columns(3).ColumnName = "IPEnd"
            End Select
        Next


        '存入檔案
        Dim ExistCount As Integer = 0
        Dim bbl As AssetManage.BBL.IPLocationDataBBL = New AssetManage.BBL.IPLocationDataBBL()
        For j As Integer = 0 To dt.Rows.Count - 1
            If Me.IPCheck(dt.Rows(j)("IPBeg").ToString().Trim(), dt.Rows(j)("IPEnd").ToString().Trim(), dt.Rows(j)("Location").ToString().Trim(), dt.Rows(j)("Site").ToString().Trim()) = False Then
                ExistCount = ExistCount + 1
            Else
                bbl.InsertIPLocationData(dt.Rows(j)("Site").ToString().Trim(), dt.Rows(j)("Location").ToString().Trim(), dt.Rows(j)("IPBeg").ToString().Trim(), dt.Rows(j)("IPEnd").ToString().Trim(), hiUID.Value, dbName)

            End If
        Next

        ShowMessage(String.Format(GetLocalResourceObject("SuccessMessage"), dt.Rows.Count - ExistCount, ExistCount, dt.Rows.Count))

    End Sub
End Class
